import java.applet.Applet;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;


public class Main {

	private static double idozitett;
	private static double intelligens;

	public static void main(String[] args) throws CloneNotSupportedException {
		
		ArrayList<Integer> waitingTime = new ArrayList<Integer>();
		//Intelligens keresztezĹ‘dĂ©s
		Intersection elso = new Intersection(waitingTime, true);
		Intersection masodik = new Intersection(waitingTime, true);
		Intersection harmadik = new Intersection(waitingTime, true);
		Intersection negyedik = new Intersection(waitingTime, true);
		Intersection otodik = new Intersection(waitingTime, true);
		
		ArrayList<Car> leftCars = new ArrayList<>();
		ArrayList<Car> rightCars = new ArrayList<>();
		ArrayList<Car> topCars = new ArrayList<>();
		ArrayList<Car> bottomCars = new ArrayList<>();
		
		Random random = new Random();
		
		for (int i = 0; i < random.nextInt(200)+50; i++) {
			leftCars.add( new Car() );
		}
		
		for (int i = 0; i < random.nextInt(200)+50; i++) {
			rightCars.add( new Car() );
		}
		
		for (int i = 0; i < random.nextInt(200)+50; i++) {
			topCars.add( new Car() );
		}
		
		for (int i = 0; i < random.nextInt(200)+50; i++) {
			bottomCars.add( new Car() );
		}
		
		ArrayList<Car> leftCars2 = new ArrayList<>();
	    for(Car car: leftCars) leftCars2.add( (Car) car.clone());
		ArrayList<Car> rightCars2 = new ArrayList<>();
	    for(Car car: rightCars) rightCars2.add( (Car) car.clone());
	    ArrayList<Car> topCars2 = new ArrayList<>();
	    for(Car car: topCars) topCars2.add( (Car) car.clone());
	    ArrayList<Car> bottomCars2 = new ArrayList<>();
	    for(Car car: bottomCars) bottomCars2.add( (Car) car.clone());
		
		
		elso.fillArrays(leftCars, rightCars, topCars, bottomCars);
		
		
		HashMap<Road, Intersection> intersections1 = new HashMap<Road, Intersection>();
		intersections1.put(Road.NORTH, masodik);
		intersections1.put(Road.SOUTH, harmadik);
		intersections1.put(Road.WEST, negyedik);
		intersections1.put(Road.EAST, otodik);
		elso.setNeighbours(intersections1);
		
		HashMap<Road, Intersection> intersections2 = new HashMap<Road, Intersection>();
		intersections2.put(Road.SOUTH, elso);
		masodik.setNeighbours(intersections2);
		
		HashMap<Road, Intersection> intersections3 = new HashMap<Road, Intersection>();
		intersections3.put(Road.NORTH, elso);
		harmadik.setNeighbours(intersections3);
		
		HashMap<Road, Intersection> intersections4 = new HashMap<Road, Intersection>();
		intersections4.put(Road.EAST, elso);
		negyedik.setNeighbours(intersections4);
		
		HashMap<Road, Intersection> intersections5 = new HashMap<Road, Intersection>();
		intersections5.put(Road.WEST, elso);
		otodik.setNeighbours(intersections5);
		
		elso.start();
		masodik.start();
		harmadik.start();
		negyedik.start();
		otodik.start();
		
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		
		String input = "";
		while( !input.equalsIgnoreCase("quit") ) {
			try {
				input = reader.readLine();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		elso.quit();
		masodik.quit();
		harmadik.quit();
		negyedik.quit();
		otodik.quit();

		
		//Nem intelligens keresztezĹ‘dĂ©s
		ArrayList<Integer> waitingTime2 = new ArrayList<Integer>();
		
	    Intersection elso2 = new Intersection(waitingTime2, false);
		Intersection masodik2 = new Intersection(waitingTime2, false);
		Intersection harmadik2 = new Intersection(waitingTime2, false);
		Intersection negyedik2 = new Intersection(waitingTime2, false);
		Intersection otodik2 = new Intersection(waitingTime2, false);
		
		
		
		elso2.fillArrays(leftCars2, rightCars2, topCars2, bottomCars2);
		
		
		intersections1 = new HashMap<Road, Intersection>();
		intersections1.put(Road.NORTH, masodik2);
		intersections1.put(Road.SOUTH, harmadik2);
		intersections1.put(Road.WEST, negyedik2);
		intersections1.put(Road.EAST, otodik2);
		elso2.setNeighbours(intersections1);
		
		intersections2 = new HashMap<Road, Intersection>();
		intersections2.put(Road.SOUTH, elso2);
		masodik2.setNeighbours(intersections2);
		
		intersections3 = new HashMap<Road, Intersection>();
		intersections3.put(Road.NORTH, elso2);
		harmadik2.setNeighbours(intersections3);
		
		intersections4 = new HashMap<Road, Intersection>();
		intersections4.put(Road.EAST, elso2);
		negyedik2.setNeighbours(intersections4);
		
		intersections5 = new HashMap<Road, Intersection>();
		intersections5.put(Road.WEST, elso2);
		otodik2.setNeighbours(intersections5);
		
		elso2.start();
		masodik2.start();
		harmadik2.start();
		negyedik2.start();
		otodik2.start();
				
		input = "";
		while( !input.equalsIgnoreCase("quit") ) {
			try {
				input = reader.readLine();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		elso2.quit();
		masodik2.quit();
		harmadik2.quit();
		negyedik2.quit();
		otodik2.quit();
	    
	    
		System.out.println("Véget ért a szimuláció");
		System.out.println("Átlagos várakozási idő:");
		double avg = 0;
		for (Integer double1 : waitingTime) {
			avg += double1;
		}
		System.out.println(avg/waitingTime.size());
		intelligens = avg/waitingTime.size();
		
		avg = 0;
		for (Integer double1 : waitingTime2) {
			avg += double1;
		}
		
		System.out.println(avg/waitingTime2.size());
		
		idozitett = avg/waitingTime2.size();
		
		JFrame frame = new JFrame(
				"Mesterséges Intelligencia projectbeadandó - Kondákor Balázs & Szalontay Marcell Zoltán");
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent event) {
				System.exit(0);
			}
		});
		Applet applet = new CrossSimulation();
		applet.init();
		
		frame.add(applet);
		frame.setSize(600, 500);

		JMenuBar menubar = new JMenuBar();
		frame.setJMenuBar(menubar);
		//menubar.setBackground(Color.CYAN);

		JMenu menu = new JMenu("File");
		menu.setMnemonic(KeyEvent.VK_F);
		menubar.add(menu);
		JMenuItem exititem = new JMenuItem("Exit", KeyEvent.VK_E);
		menu.add(exititem);
		
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
		((CrossSimulation) applet).animate();
		
		exititem.addActionListener(new ActionListener() {
		
			@Override
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);

			}
		});

	}

	public static double getIdozitett() {
		return idozitett;
	}

	public static double getIntelligens() {
		return intelligens;
	}

}
